﻿<!DOCTYPE html>
<html>
<head>
    <title>读取本地文件</title>
    <style>
        .line-item {
            padding: 5px;
            margin: 2px 0;
            border-left: 3px solid #007bff;
            background: #f8f9fa;
        }
        .line-number {
            font-weight: bold;
            color: #007bff;
        }
    </style>
</head>
<body>
    <input type="file" id="fileInput" accept=".txt,.csv,.log,.text" />
    <div id="output"></div>

    <script>
        document.getElementById('fileInput').addEventListener('change', function(event) {
            const file = event.target.files[0];
            if (!file) return;

            const reader = new FileReader();
            
            reader.onload = function(e) {
                const content = e.target.result;
                // 处理不同操作系统的换行符
                const lines = content.split(/\r\n|\r|\n/);
                currentIndex = 0;
                //displayLines(lines);
                processLines(lines);

            };
            
            reader.onerror = function(e) {
                console.error('文件读取错误:', e.target.error);
                alert('文件读取失败');
            };
            
            reader.readAsText(file, 'UTF-8');
        });
		let currentIndex = 0;
        function ends() {
            const lineDiv = document.createElement('div');
            lineDiv.className = 'line-item1';
            lineDiv.innerHTML = `
                    <span class="line-number">结束了</span>:

                `;
            output.appendChild(lineDiv);

        }
        function displayLines(lines) {
            const output = document.getElementById('output');
            output.innerHTML = `<h3>文件内容 (共 ${lines.length} 行):</h3>`;
            
            lines.forEach((line, index) => {
                const lineDiv = document.createElement('div');
                lineDiv.className = 'line-item';
                lineDiv.innerHTML = `
                    <span class="line-number">${index + 1}</span>: 
                    ${line || '<em style="color: #999;">空行</em>'}
                `;
                output.appendChild(lineDiv);
            });
        }
		 function displayLinesMeiPao(lines,num) {
            const lineDiv = document.createElement('div');
                lineDiv.className = 'line-item';
                lineDiv.innerHTML = `
                    <span class="line-number">行数${num }</span>:
                    <span class="line-number">错误${lines }</span>
                `;
                output.appendChild(lineDiv);
        }
		function displayLinesManey(lines,num) {
            const lineDiv = document.createElement('div');
                lineDiv.className = 'line-item';
                lineDiv.innerHTML = `
                    <span class="line-number">行数${num }</span>:
                    <span class="line-number">钱${lines }</span>

                `;
                output.appendChild(lineDiv);
        }
        let index = 0;
        function processLines(lines) {
            if (currentIndex >= lines.length) {
            console.log('=== 文件处理完成 ===');
            return;}
            index = 0;

            function next() {
                if (index >= lines.length) {
                    console.log('循环结束');
                    ends();
                    return;
                }
                // console.log(`执行 ${lines[index]}`);
                findmaney(lines[index])
                index++;

                // 500ms后执行下一次
                setTimeout(next, 500);
                // console.log("第几行"+index);
            }
            next();
            
        //     // 逐行处理数据
		// 		 const lineNumber = currentIndex + 1;
        //          const trimmedLine = lines[currentIndex].trim();
        //
        //          console.log(`第${lineNumber}行: "${lines[currentIndex]}"`);
        //
        //             // 示例：处理逻辑
        //             if (trimmedLine === '') {
        //               console.log(`  → 第${lineNumber}行是空行`);
        //          } else if (trimmedLine.startsWith('#')) {
        //     console.log(`  → 第${lineNumber}行是注释`);
        // } else if (trimmedLine.includes('error')) {
        //     console.log(`  → 第${lineNumber}行包含错误信息`);
        // }
        //
        // currentIndex++;
        
        // 500ms后处理下一行
        //     setTimeout(processLines, 1000);
		//     processLines(lines);
        }
		
		
		
		
		function findmaney(addresses){
			const url = 'https://blockchain.info/balance?active=' + addresses;

            fetch(url).then(response => response.json()).then(data => {
                 // console.log('所有地址数据:', data);
                    if (data.error) {
                        let i =index;
                        displayLinesMeiPao(addresses,i)
                        return;
                    }
                // 找出所有余额 > 0 的地址
                const addressesWithBalance = Object.entries(data).filter( ([address,info]) => info.final_balance > 0);

                    // console.log('余额 > 0 的地址:');
                 addressesWithBalance.forEach( ([address,info]) => {console.log('余额 > 0 ');
                                let j =index;
                                console.log(`📍 ${address}: ${info.final_balance} satoshis` +'第几行：'+j);
                                 displayLinesManey(address,j)
								// alert("第几行："+index + "地址："+addresses);
								
                 });

                // 如果没有余额 > 0 的地址
                 // if (addressesWithBalance.length === 0) {
                //     console.log('没有找到余额 > 0 的地址');
                 // }
                }
                ).catch(error =>  { console.log('错误：'+addresses +'行数'+index);displayLinesMeiPao(addresses,index)} );
		
		
		}
		
		
    </script>
</body>
</html>